/// <summary>
/// These functions are managed functions which perform
/// the translation from the low-level functions in API.h
/// to the high-level managed code in Interact.h and
/// InterProcess.cpp
/// </summary>

#include "Wrap.h"
#include "GM.h"
#include "Interact.h"

#pragma managed

namespace Wrap
{
	/// <summary>
	/// Returns whether or not RoketPack is currently running
	/// and available to recieve API requests.
	/// <summary>
	/// <returns>True if RoketPack can be contacted, false otherwise.</returns>
	double RoketPackIsAvailable()
	{
		API::Interact ^ interact = gcnew API::Interact("roketpack_" + Environment::UserName);

		try
		{
			if (interact->RoketPackIsAvailable())
				return GM_True;
			else
				return GM_False;
		}
		catch (System::Runtime::Remoting::RemotingException ^)
		{
			return GM_False;
		}
	}

	/// <summary>
	/// Returns whether the specified application has one or more
	/// components currently installed.
	/// <summary>
	/// <returns>True if there are one or more components installed, false if not.  Returns -1 if RoketPack could not be contacted.</returns>
	/// <param name="repository">The repository URL that contains the application.</param>
	/// <param name="application">The application ID.</param>
	double ApplicationIsInstalled(const char* repository, const char* application)
	{
		API::Interact ^ interact = gcnew API::Interact("roketpack_" + Environment::UserName);

		try
		{
			if (interact->ApplicationIsInstalled(
							gcnew System::String(repository),
							gcnew System::String(application)
							))
				return GM_True;
			else
				return GM_False;
		}
		catch (System::Runtime::Remoting::RemotingException ^)
		{
			return -1.0f;
		}
	}

	/// <summary>
	/// Returns whether the specified component is currently installed.
	/// <summary>
	/// <returns>True if the component is installed, false if not.  Returns -1 if RoketPack could not be contacted.</returns>
	/// <param name="repository">The repository URL that contains the application.</param>
	/// <param name="application">The application ID that contains the component.</param>
	/// <param name="component">The component ID.</param>
	double ComponentIsInstalled(const char* repository, const char* application, const char* component)
	{
		API::Interact ^ interact = gcnew API::Interact("roketpack_" + Environment::UserName);

		try
		{
			if (interact->ComponentIsInstalled(
							gcnew System::String(repository),
							gcnew System::String(application),
							gcnew System::String(component)
							))
				return GM_True;
			else
				return GM_False;
		}
		catch (System::Runtime::Remoting::RemotingException ^)
		{
			return -1.0f;
		}
	}

	/// <summary>
	/// Instructs RoketPack to begin the installation of a
	/// component.  Returns whether or not the installation
	/// was successfully started (it's an asynchronous function).
	/// A possible reason for a false value to be returned is
	/// if the user denied the request to install the
	/// component.
	/// <summary>
	/// <returns>Whether the installation was started.</returns>
	/// <param name="repository">The repository URL that contains the application.</param>
	/// <param name="application">The application ID that contains the component.</param>
	/// <param name="component">The component ID.</param>
	double ComponentInstall(const char* repository, const char* application, const char* component)
	{
		API::Interact ^ interact = gcnew API::Interact("roketpack_" + Environment::UserName);

		try
		{
			if (interact->ComponentInstall(
							gcnew System::String(repository),
							gcnew System::String(application),
							gcnew System::String(component)
							))
				return GM_True;
			else
				return GM_False;
		}
		catch (System::Runtime::Remoting::RemotingException ^)
		{
			return GM_False;
		}
	}

	/// <summary>
	/// Instructs RoketPack to begin the update of a
	/// component.  Returns whether or not the update
	/// was successfully started (it's an asynchronous function).
	/// <summary>
	/// <returns>Whether the update was started.</returns>
	/// <param name="repository">The repository URL that contains the application.</param>
	/// <param name="application">The application ID that contains the component.</param>
	/// <param name="component">The component ID.</param>
	double ComponentUpdate(const char* repository, const char* application, const char* component)
	{
		API::Interact ^ interact = gcnew API::Interact("roketpack_" + Environment::UserName);

		try
		{
			if (interact->ComponentUpdate(
							gcnew System::String(repository),
							gcnew System::String(application),
							gcnew System::String(component)
							))
				return GM_True;
			else
				return GM_False;
		}
		catch (System::Runtime::Remoting::RemotingException ^)
		{
			return GM_False;
		}
	}

	/// <summary>
	/// Instructs RoketPack to begin the uninstallation of a
	/// component.  Returns whether or not the uninstallation
	/// was successfully started (it's an asynchronous function).
	/// A possible reason for a false value to be returned is
	/// if the user denied the request to uninstall the
	/// component.
	/// <summary>
	/// <returns>Whether the uninstallation was started.</returns>
	/// <param name="repository">The repository URL that contains the application.</param>
	/// <param name="application">The application ID that contains the component.</param>
	/// <param name="component">The component ID.</param>
	double ComponentUninstall(const char* repository, const char* application, const char* component)
	{
		API::Interact ^ interact = gcnew API::Interact("roketpack_" + Environment::UserName);

		try
		{
			if (interact->ComponentUninstall(
							gcnew System::String(repository),
							gcnew System::String(application),
							gcnew System::String(component)
							))
				return GM_True;
			else
				return GM_False;
		}
		catch (System::Runtime::Remoting::RemotingException ^)
		{
			return GM_False;
		}
	}

	/// <summary>
	/// Returns whether or not the version on the computer is
	/// up-to-date (i.e. the latest version available in the
	/// repository).
	/// <summary>
	/// <returns>True or false depending on whether the component is up-to-date, -1 if RoketPack could not be contacted.</returns>
	/// <param name="repository">The repository URL that contains the application.</param>
	/// <param name="application">The application ID that contains the component.</param>
	/// <param name="component">The component ID.</param>
	double VersionIsLatest(const char* repository, const char* application, const char* component)
	{
		API::Interact ^ interact = gcnew API::Interact("roketpack_" + Environment::UserName);

		try
		{
			if (interact->VersionIsLatest(
							gcnew System::String(repository),
							gcnew System::String(application),
							gcnew System::String(component)
							))
				return GM_True;
			else
				return GM_False;
		}
		catch (System::Runtime::Remoting::RemotingException ^)
		{
			return -1.0f;
		}
	}

	/// <summary>
	/// Calls a URL in RoketPack.  This is the same method that
	/// is used to handle the roket: protocol requests from the
	/// web browser, however, unlike requests originating from
	/// the roket: protocol, warning dialogs will be issued
	/// during install or uninstall requests (just as if using
	/// the API functions), and they will be denied automatically
	/// if the repository in the URL is not in the user's list
	/// of installed repositories.
	/// <summary>
	/// <returns>True is the request was sent (but does not necessarily indicate it succeeded), or false if RoketPack could not be contacted.</returns>
	/// <param name="repository">The repository URL that contains the application.</param>
	/// <param name="application">The application ID that contains the component.</param>
	/// <param name="component">The component ID.</param>
	double MiscellaneousCallUrl(const char* url)
	{
		API::Interact ^ interact = gcnew API::Interact("roketpack_" + Environment::UserName);

		try
		{
			if (interact->MiscellaneousCallURL(gcnew System::String(url)))
				return GM_True;
			else
				return GM_False;
		}
		catch (System::Runtime::Remoting::RemotingException ^)
		{
			return GM_False;
		}
	}
}